<template>
    <el-form-item :label-width="item.labelWidth?item.labelWidth:'130px'" :label="item.title" :prop="rules?item.field:''">
      <span slot="label">{{item.title}}
        <el-tooltip v-if="item.info" :content="item.info" placement="top">
          <span class="fas fa-info-circle color"></span>
        </el-tooltip>
      </span>
      <el-select @change="change" v-if="!item.isParams" v-model="form[item.field]" :disabled="disabled" filterable placeholder="请选择" style="width: 100%;">
        <el-option
          v-for="obj in item.options"
          :key="obj.value"
          :label="obj.name"
          :value="obj.value">
        </el-option>
      </el-select>
     <el-select @change="change" v-if="item.isParams===true" v-model="form[item.field]"  :disabled="disabled" filterable placeholder="请选择" style="width: 100%;">
        <el-option
          v-for="obj in list"
          :key="obj.itemCode+''"
          :label="obj.itemText"
          :value="obj.itemCode+''">
        </el-option>
      </el-select>
    </el-form-item>
</template>

<script>
import { getSelected } from '../../../api/common';
import store from '@/store';
export default {
  props: ['item', 'form', 'rules', 'disabled', 'isParams', 'paramsId'],
  data() {
    return {
      list: []
    };
  },
  mounted: function() {
    if (this.item.init) {
      this.item.init(this.form, this.item);
    }
    if (this.item.isParams === true) {
      const cacheParams = this.$store.state.params.params;
      /**
       * 如果在缓存中，直接获取缓存中数据
       */
      if (cacheParams[this.item.paramsId]) {
        this.list = cacheParams[this.item.paramsId];
        return;
      }
      getSelected(this.item.paramsId).then(response => {
        if (!response.data) {
          this.list = [];
        } else {
          this.list = response.data.items;
          /**
           * 参数缓存
           */
          store.dispatch('addParams', response.data);
        }
      });
    }
  },
  methods: {
    change(v) {
      if (this.item.itemChange) {
        this.item.itemChange(this.form, this.item, v);
      }
    }
  }
};
</script>

